/*
 * iap.h
 *
 *  Created on: 13-02-2012
 *      Author: Mateusz
 */

#ifndef IAP_H_
#define IAP_H_

extern uint8_t user_flash_erased;

typedef enum
{
	PREPARE_SECTOR_FOR_WRITE = 50,
	COPY_RAM_TO_FLASH = 51,
	ERASE_SECTOR = 52,
	BLANK_CHECK_SECTOR = 53,
	READ_PART_ID = 54,
	READ_BOOT_VER = 55,
	COMPARE = 56,
	REINVOKE_ISP = 57
} IAP_Command_Code;

#define CMD_SUCCESS 0
#define IAP_ADDRESS 0x1FFF1FF1

//#define COMPUTE_BINARY_CHECKSUM

#define FLASH_BUF_SIZE 512
#define USER_START_SECTOR 7
#define MAX_USER_SECTOR 7
#define MAX_FLASH_SECTOR 8

/* Define start address of each Flash sector */
#define SECTOR_0_START      0x00000000
#define SECTOR_1_START      0x00001000
#define SECTOR_2_START      0x00002000
#define SECTOR_3_START      0x00003000
#define SECTOR_4_START      0x00004000
#define SECTOR_5_START      0x00005000
#define SECTOR_6_START      0x00006000
#define SECTOR_7_START      0x00007000

/* Define end address of each Flash sector */
#define SECTOR_0_END        0x00000FFF
#define SECTOR_1_END        0x00001FFF
#define SECTOR_2_END        0x00002FFF
#define SECTOR_3_END        0x00003FFF
#define SECTOR_4_END        0x00004FFF
#define SECTOR_5_END        0x00005FFF
#define SECTOR_6_END        0x00006FFF
#define SECTOR_7_END        0x00007FFF

extern const unsigned sector_start_map[];
extern const unsigned sector_end_map[];

#define USER_FLASH_START (sector_start_map[USER_START_SECTOR])
#define USER_FLASH_END	 (sector_end_map[MAX_USER_SECTOR])
#define USER_FLASH_SIZE  ((USER_FLASH_END - USER_FLASH_START) + 1)

unsigned write_flash(unsigned * dst, char * src, unsigned no_of_bytes);
void erase_user_flash(void);

#endif /* IAP_H_ */
